package com.jgp.glmeeting.service.impl;

import com.jgp.glmeeting.model.Invoice;
import com.jgp.glmeeting.repository.InvoiceRepository;
import com.jgp.glmeeting.service.InvoiceService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.query.OrderList;
import com.jgp.sys.common.query.QueryFilterList;
import com.jgp.sys.ui.Pager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceRepository invoiceRepository;
    @Autowired
    private ParticipantsService participantsService;



    /**
     *批量添加发票
     */
    @Transactional
    @Override
    public void insertList(List<Invoice> invoiceList) {
        if(CollectionUtils.isNotEmpty(invoiceList)){
            invoiceRepository.createInBatch(invoiceList);
            List<String> participantsIds = new ArrayList<>();
            for (Invoice in : invoiceList) {
                participantsIds.add(in.getParticipantsId());
            }
            participantsService.updateInvoicedByIds(participantsIds);
        }
    }

    /**
     * 查询所有并且分页啊
     */
    @Override
    public List<Invoice> queryInvoice(Invoice i, Pager pager) {
        OrderList orders=new OrderList();
        orders.addOrder("createDatetime","desc");
        String workUnit=i.getWorkUnit();
        QueryFilterList queryFilterList=new QueryFilterList();
        if(StringUtils.isNotBlank(workUnit)){
            queryFilterList.addFilter("workUnit","eq",workUnit);
        }
        return invoiceRepository.read(queryFilterList,orders,pager);
    }

    @Override
    public List<Invoice> queryAll() {
        QueryFilterList queryFilterList=new QueryFilterList();

        return invoiceRepository.read(queryFilterList);
    }
}
